package com.hong.taotaomall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hong.taotaomall.entity.Category;
import com.hong.taotaomall.entity.Result;
import com.hong.taotaomall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品及套装分类 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-03-25
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @PostMapping
    public Result<String> save(@RequestBody Category category){
//        log.info("新增分类：{}",category);
        categoryService.save(category);
        return Result.success("新增分类成功");
    }

    @GetMapping("/page")
    public Result<Page> save(int page, int pageSize){
        Page<Category> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();

        lqw.orderByAsc(Category::getSort);

        categoryService.page(pageInfo,lqw);

        return Result.success(pageInfo);
    }

    @DeleteMapping
    public Result<String> delete(Long ids){
        categoryService.remove(ids);
        return Result.success("分类信息删除成功");
    }

    @PutMapping
    public Result<String> update(@RequestBody Category category){
        log.info("分类信息修改");
        categoryService.updateById(category);
        return Result.success("分类信息修改成功");
    }

    @GetMapping("/list")
    public Result<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(category.getType() != null ,Category::getType,category.getType());
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return Result.success(list);
    }
}
